package com.example.hospitalManage.controller.admin.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hospitalManage.entity.HospitalSet;
import com.example.hospitalManage.service.HospitalSetService;
import com.example.hospitalManage.utils.MD5;
import com.example.hospitalManage.utils.Response;
import com.example.hospitalManage.utils.ResponseResult;
import com.example.hospitalManage.vo.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * Description:
 * Param:
 * return:
 * Author: 彭雨胜
 * Data: 2022/12/29
 */
@RestController
@RequestMapping("/admin")
@Api(value = "v1",tags = "医院设置相关接口")
@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //定义日志对象
    public final static Logger log = LoggerFactory.getLogger(HospitalSetController.class);

    // 查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("hospitalSet")
    public ResponseResult<List<HospitalSet>> findAllHospitalSet() {
        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        return Response.createOkResp(hospitalSetList);
    }

    // 根据id删除医院设置
    @ApiOperation(value = "根据id删除医院设置")
    @DeleteMapping("hospitalSet/{id}")
    public ResponseResult removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if(flag) {
            return Response.createOkResp();
        } else {
            return Response.createFailResp("医院设置删除失败");
        }
    }

    // 条件查询带分页
    @ApiOperation(value = "条件分页查询")
    @PostMapping("hospitalSet/{current}/{limit}")
    public ResponseResult findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();  // 医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();  // 医院编号
        if(!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }

        //调用方法实现分页查询
        IPage<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);

        //返回结果
        return Response.createOkResp(pageHospitalSet);
    }

    // 添加医院设置
    @ApiOperation(value = "添加医院设置")
    @PostMapping("hospitalSet")
    public ResponseResult saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if(save) {
            return Response.createOkResp();
        } else {
            return Response.createFailResp();
        }
    }

    // 根据id获取医院设置
    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("hospitalSet/{id}")
    public ResponseResult getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Response.createOkResp(hospitalSet);
    }

    // 修改医院设置
    @PutMapping("hospitalSet")
    @ApiOperation(value = "修改医院设置")
    public ResponseResult updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag) {
            return Response.createOkResp();
        } else {
            return Response.createFailResp();
        }
    }

    // 批量删除医院设置
    @DeleteMapping("hospitalSet/batchRemove")
    @ApiOperation(value = "批量删除医院设置")
    public ResponseResult batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return Response.createOkResp();
    }

}
